package com.grape.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grape.utils.StringUtils;
import com.grape.vo.BaseVo;
import com.grape.vo.RolePermissionVO;
import com.grape.wrapper.BaseWrapper;
import com.grape.domain.SysRolePermission;
import com.grape.mapper.SysRolePermissionMapper;
import com.grape.service.ISysRolePermissionService;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 角色权限中间表 服务实现类
 *
 * @author  putao
 * @email   2628279194@qq.com
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

    private static final Logger logger = LoggerFactory.getLogger(SysRolePermissionServiceImpl.class);

    private final SysRolePermissionMapper sysRolePermissionMapper;

    /**
     * 分页
     * @param baseVo 统一分页对象
     * @return 角色权限中间表集合
     */
    @Override
    public List<SysRolePermission> listData(BaseVo baseVo) {
        QueryWrapper<SysRolePermission> wrapper = new BaseWrapper<SysRolePermission>().init();
        wrapper.like(Strings.isNotBlank(baseVo.getSearch()), "name", baseVo.getSearch());
        wrapper.eq(StringUtils.isNotBlank(baseVo.getColumnName())
                && StringUtils.isNotBlank(baseVo.getSelect()), baseVo.getColumnName(), baseVo.getSelect());
        return this.list(wrapper);
    }

    /**
     * 根据id查询详情
     * @param id    角色权限id
     * @return      角色权限对象
     */
    @Override
    public SysRolePermission detail(String id) {
        return this.getById(id);
    }

    /**
     * 修改角色的权限
     * @param rolePermissionVo  角色和权限数据
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void updateRolePermissionList(RolePermissionVO rolePermissionVo) {
        int count = sysRolePermissionMapper.delete(new QueryWrapper<SysRolePermission>().eq("role_id", rolePermissionVo.getRoleId()));
        logger.info("执行删除角色权限数据操作，共删除: {} 条数据", count);
        List<String> permissionIdList = rolePermissionVo.getPermissionIdList();
        List<SysRolePermission> rolePermissionList = new ArrayList<>();
        for (String permissionId : permissionIdList) {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setId(IdUtil.getSnowflake(31, 31).nextIdStr());
            sysRolePermission.setRoleId(rolePermissionVo.getRoleId());
            sysRolePermission.setPermissionId(permissionId);
            rolePermissionList.add(sysRolePermission);
        }
        this.saveBatch(rolePermissionList);
    }
}
